home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / comm / tlx_sq21.zip / GLOBAL.SLT < prev    next >
Text File  |  1992-03-20  |  9KB  |  256 lines

  1. //-----------------------------------------------------------
  2. // GLOBAL.SL? Save global variables in Telix.
  3. //
  4. // Calling sequence:
  5.  
  6. // Read the variable "NAME", result in <variable string>:
  7.  
  8. //    call ("GLOBAL","R","NAME",<variable string>);
  9. //    If "NAME" is not found, an empty string is returned.
  10.  
  11. // Write the string <variable string> into "NAME":
  12.  
  13. //    call ("GLOBAL","W","NAME",<variable string>);
  14. //    If "NAME" can not be stored (not enough room) the variable
  15. //    will not be stored, and the script will return the code -1.
  16.  
  17. // Delete a variable:
  18.  
  19. //    call ("GLOBAL","W","NAME",0);      // (the "0" must be included)
  20. // or call ("GLOBAL","D","NAME",0);      // (the "0" may be omitted)
  21.  
  22. // In variable names, case is insignificant. The variables may be
  23. // *any* string, except the length is limited to 80 characters 
  24. // The script can store up to 40 variables of length <=20 characters,
  25. // and 10 variables of 20 < length <=80 characters,
  26. // Make sure that the longest length you'll ever use is specified
  27. // the first time you define a variable. Otherwise, if you want to
  28. // modify a <20 char. variable to >20 characters, you must delete
  29. // the original first.
  30. //-----------------------------------------------------------
  31.  
  32. // If you have suggestions for improving this script, please suggest
  33. // improvements to me via old-fashioned snail-mail to:
  34.  
  35. // Author:  Inge Vabekk
  36. //          Hamangskogen 108
  37. //          N-1300 SANDVIKA
  38. //          NORWAY
  39. //          tel. (472) 546 396
  40.  
  41. str shortnames[360]     // Table of names for 60 short strings of 6.
  42.    ,longnames [60]      // Table of names for 10 long strings of 6.
  43.    ,shortstrings [1260] // 60 strings of length 21
  44.    ,longstrings [810]   // 10 strings of length 81
  45.    ,helpname [6]        // Temporary for name
  46.    ,spaces[6]= "      " // Store spaces.
  47.    ,helpvar [81]        // Temporary for string 
  48.    ;                    // with (room for terminating zero)
  49.  
  50. int next_short_entry=0
  51.    ,next_long_entry=0
  52.    ,namelen             // Length of names
  53.    ,longstringlen=81    // Length of strings.
  54.    ,shortstringlen=21
  55.    ,varlen              // Length of current string.
  56.    ,long                // Long/short indocator    
  57.    ,get = 0             // Code to GET a variable.
  58.    ,put = 1             // Code to PUT a variable.
  59.    ;
  60.  
  61. //-----------------------------------------------------------
  62. // Script starts here.
  63. //-----------------------------------------------------------
  64.  
  65. main (str rwrite, str name, str varname)
  66. {
  67. int i, j, l1, l2;
  68.  
  69.   namelen = strmaxlen(helpname);
  70.   varlen = strlen(varname);
  71.  
  72.   if (rwrite=="R")
  73.     i = read (name,varname);             // Read the value.
  74.  
  75.   else if (rwrite=="W")
  76.   { if (strlen(varname) > 0)         
  77.       i = write (name,varname);          // write this name.       
  78.     else
  79.       i = write (name,"");               // Delete.
  80.  
  81.     if (i < 0) 
  82.     { helpvar = "T²: Global script has no room for variable ";
  83.       strcat (helpvar,name);
  84.       strcat (helpvar," = ");
  85.       strcat (helpvar,varname);
  86.       l1 = 40-strlen(helpvar)/2 - 1;
  87.       l2 = l1+strlen(helpvar);
  88.       clear_scr();
  89.       box (l1-6,7,l2+5,16,176,1,15);
  90.       box (l1-4,8,l2+3,15,177,1,15);
  91.       box (l1-2,9,l2+1,14,178,1,15);
  92.       pstraxy (helpvar,l1,11,79);
  93.       pstraxy ("Please report to author!",28,12,79);
  94.       tone (100,200);
  95.       gotoxy (0,24);
  96.     }
  97.   }
  98.   else if (rwrite=="D")
  99.     i = write (name,"");
  100.    
  101.   else if (rwrite=="L")                  // List all names.
  102.   { prints ("ALL SHORT GLOBAL VARIABLES:");
  103.     for (i=j=0; i<next_short_entry; i=i+namelen) 
  104.     { substr (shortstrings,j,shortstringlen,helpvar);
  105.       substr (shortnames,i,namelen,helpname);
  106.       printsc(helpname);
  107.       printsc (":");
  108.       prints (helpvar);
  109.       j = j+shortstringlen;
  110.     }
  111.     prints ("ALL LONG GLOBAL VARIABLES:");
  112.     for (i=j=0; i<next_long_entry; i=i+namelen) 
  113.     { substr (longstrings,j,longstringlen,helpvar);
  114.       substr (longnames,i,namelen,helpname);
  115.       printsc(helpname);
  116.       printsc (":");
  117.       prints (helpvar);
  118.       j = j+longstringlen;
  119.     }
  120.   }
  121.   return (i);                            // Return with code.
  122. }
  123.  
  124. //-----------------------------------------------------------
  125. // Read a variable.
  126. //-----------------------------------------------------------
  127.  
  128. read (str name, str varname)
  129. {
  130.   move (name);                           // Move name to global
  131.   return (getput (0,varname));           // and read.
  132. }
  133.  
  134. //-----------------------------------------------------------
  135. // Write a variable.
  136. //-----------------------------------------------------------
  137.  
  138. write(str name, str varname)
  139. {
  140.   move (name);                           // Move name to global
  141.   return (getput (1,varname));           // and write.
  142. }
  143.  
  144. //-----------------------------------------------------------
  145. // Move name to global area.
  146. //-----------------------------------------------------------
  147.  
  148. move(str name)
  149. {
  150.   helpname = name;                       // Move name to global.
  151.   strupper(helpname);                    // Make upper characters
  152.   strcat (helpname,spaces);              // fill with spaces
  153. }
  154.  
  155. //-----------------------------------------------------------
  156. // Read or write a name to or from the storage area.
  157. //-----------------------------------------------------------
  158.  
  159. getput (int write, str varname)
  160. {
  161. int c, i, j, exist;
  162.  
  163.   exist = strpos (longnames,helpname,0); // Check if name is in table.
  164.   if (exist >= 0)
  165.     long = 1;
  166.   else
  167.   { long = 0;
  168.     exist = strpos (shortnames,helpname,0); // Check if name is in table.
  169.   }
  170.   if (exist < 0)                         // Non-existent?
  171.   { if (!write)                          // Read?
  172.     { varname = "";                      // Yes. Nothing there.
  173.       return(-1);
  174.     }
  175.     long = varlen > 20;
  176.     if (long)                            // Add long variable
  177.     { exist = strpos (longnames,spaces,0);// Deleted variable found?
  178.       if (exist >= 0)                    // Must be at correct position!
  179.         exist = ((exist+namelen-1)/namelen)*namelen;
  180.       else                               // No, expand table.
  181.       { if (next_long_entry >= strmaxlen(longnames))
  182.         { status_wind ("T²: Overflow in long name table!",30);
  183.           return (-1);
  184.         }
  185.         exist = next_long_entry;
  186.         next_long_entry = next_long_entry + namelen;
  187. //!!!!!!!!!!!!!!!!!!!!!!!!
  188. //      printsc ("Long: ");
  189. //      printn (Next_long_entry);
  190. //      prints ("");
  191.       }
  192.                                          // Move all characters.  
  193.       copychrs (helpname,longnames,exist,namelen);
  194.     }
  195.     else                                 // Add short variable
  196.     { exist = strpos (shortnames,spaces,0);// Deleted variable found?
  197.       if (exist >= 0)                    // Must be at correct position!
  198.         exist = ((exist+namelen-1)/namelen)*namelen;
  199.       else                               // No, expand table.
  200.       { if (next_short_entry >= strmaxlen(shortnames))
  201.         { status_wind ("T²: Overflow in short name table!",30);
  202.           return (-1);
  203.         }
  204.         exist = next_short_entry;
  205.         next_short_entry = next_short_entry + namelen;
  206.       }
  207.                                        // Move all characters.  
  208.       copychrs (helpname,shortnames,exist,namelen);
  209.     }
  210.   }
  211.  
  212. // "exist"   contains start subscript for the variable name.
  213. // "j"       contains start subscript for its contents.
  214. // "long"=1; Variable is in "longstrings
  215. // "long"=0; Variable is in "shortstrings
  216.  
  217.   if (long)
  218.   { j = (exist/namelen)*longstringlen;   // Find start position.
  219.  
  220.     if (write)                           // Write?
  221.     { if (strlen(varname)==0)            // Yes. Delete?
  222.         copychrs (spaces                 // Write over with spaces.
  223.           ,longnames,exist,namelen);
  224.       else
  225.         copystr (varname                 // Copy name.
  226.           ,longstrings,j,longstringlen-1);
  227.     }
  228.     else                                 // Read:
  229.       substr (longstrings                // Copy variable.
  230.         ,